package cn.jbolt.wechat.admin.media;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.enumutil.JBoltEnum;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.core.permission.UnCheck;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.wechat.common.WechatMediaType;
import cn.jbolt.wechat.model.WechatMedia;
import cn.jbolt.wechat.model.WechatMpinfo;
import cn.jbolt.wechat.permission.WechatPermissionKey;
import cn.jbolt.wechat.service.JBoltWechatConfigService;
import cn.jbolt.wechat.service.JBoltWechatMediaService;
import cn.jbolt.wechat.service.JBoltWechatMpinfoService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.Page;

/**
 * 微信公众平台素材库管理
 * @ClassName:  WechatMediaAdminController   
 * @author: JFinal学院-小木 QQ：909854136 
 * @date:   2019年6月25日   
 */
@CheckPermission(WechatPermissionKey.WECHAT_MEDIA)
@Path("/api/admin/wechatMedia")
@UnCheckIfSystemAdmin
public class WechatMediaAdminController extends JBoltApiBaseController {
	@Inject
	private JBoltWechatMediaService service;
	@Inject
	private JBoltWechatMpinfoService wechatMpinfoService;
	@Inject
	private JBoltWechatConfigService wechatConfigService;


	public void datas(@Para(PAGE_PARAM_NAME) Integer page, @Para(value = PAGESIZE_PARAM_NAME, defaultValue = "20") Integer pageSize,
					  @Para(KEYWORDS_PARAM_NAME) String keywords, @Para("mpId") Long mpId, @Para(TYPE_PARAM_NAME) String type) {
		WechatMpinfo mpinfo=wechatMpinfoService.findById(mpId);
		if(mpinfo==null) {renderJBoltApiFail("微信公众平台信息不存在");return;}
		Page<WechatMedia> pageData = service.paginateAdminList(mpId, type, keywords, page, pageSize);

		renderJBoltApiSuccessWithData(pageData);


	}
	/**
	 * 同步上传
	 */
	@Before(WechatMediaMgrValidator.class)
	public void syncUpload() {
		Long bucketId = getLong("bucketId");
		if (notOk(bucketId)) {
			renderJBoltApiFail("未指定要上传的Bucket");
		}
		renderJBoltApiRet(service.uploadWechatMedia(getLong(0), bucketId));
	}


	@CheckPermission({WechatPermissionKey.WECHAT_AUTOREPLY_DEFAULT,WechatPermissionKey.WECHAT_AUTOREPLY_KEYWORDS,WechatPermissionKey.WECHAT_AUTOREPLY_SUBSCRIBE})
	@Before(WechatMediaMgrValidator.class)
	public void chooseIt() {
		renderJBoltApiRet(service.getReplyChooseInfo(getLong(0),getLong(1)));
	}
	@UnCheck
	public void types() {
		renderJBoltApiSuccessWithData(JBoltEnum.getEnumOptionList(WechatMediaType.class));
	}
	
	@Before(WechatMediaMgrValidator.class)
	public void syncAll() {
		Long qiniuBucketId = getLong("bucketId");
		if (notOk(qiniuBucketId)) {
			renderJBoltApiFail("未指定要同步到的七牛Bucket");
		}
		renderJBoltApiRet(service.syncAll(getLong(0)));
	}
	@Before(WechatMediaMgrValidator.class)
	public void syncNewDatas() {
		Long qiniuBucketId = getLong("bucketId");
		if (notOk(qiniuBucketId)) {
			renderJBoltApiFail("未指定要同步到的七牛Bucket");
		}
		renderJBoltApiRet(service.syncNewDatas(getLong(0)));
	}
}
